package top.ltc_cn.springboot_tools.exception;

/**
 * @author ltc-cn xiaoyangcc
 */
public class LtcException extends RuntimeException {
    private String errorCode;

    public LtcException(String msg) {
        super(msg);
        this.errorCode = getErrorCode();
    }

    public LtcException(String msg, Throwable cause) {
        super(msg, cause);
        this.errorCode = getErrorCode();
    }

    public LtcException(Throwable cause) {
        super(cause);
        this.errorCode = getErrorCode();
    }

    public LtcException(String errorCode, String msg) {
        super(msg);
        this.errorCode = errorCode;
    }

    public LtcException(String errorCode, String msg, Throwable cause) {
        super(msg, cause);
        this.errorCode = errorCode;
    }

    public String getErrorCode() {
        // 随机7位英文数字
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 7; i++) {
            int random = (int) (Math.random() * 36);
            if (random < 10) {
                sb.append(random);
            } else {
                sb.append((char) (random - 10 + 'A'));
            }
        }
        this.errorCode = sb.toString();
        return errorCode;
    }
}

